
monitor = function(url, source, datadisplayed, titleDiv, dataDiv, lastcheckDiv) {
  var WDClient = new org.WattDepot.Client(url);
  
  function sendQuery() {
    titleDiv.innerHTML = generateTitle(source, dataDisplayed);      
    lastcheckDiv.innerHTML = generateCheckDate();
    WDClient.getSourceLatestSensorData(source, handleQuery);
  }
  
  function handleQuery(response) {
	var date = WDClient.getJsonNode(response, "Timestamp");
	var value = WDClient.getPropertyValue(response, dataDisplayed);
	dataDiv.innerHTML = generateMonitor(date, value, WDClient.getUnit(dataDisplayed));
  }
    
  function generateTitle(source, dataDisplayed) {
    var html = [];
    html.push(source);
    if (dataDisplayed == "energyGeneratedToDate") {
      html.push(" Energy Generated");
    }
    else if (dataDisplayed == "energyConsumedToDate") {
      html.push(" Energy Consumed");
    }
    else if (dataDisplayed == "powerGenerated") {
      html.push(" Power Generated");
    }
    else if (dataDisplayed == "powerConsumed") {
      html.push(" Power Consumed")
    }
    else {
      html.push(dataDisplayed);
    }
    return html.join('');
  }

  function generateMonitor(date, value, unit) {
    var html = [];

    value = parseFloat(value);
    html.push('<p><font style=\"font-size:1.2em; font-weight:bold; font-family:arial,sans-serif\">');
    html.push(value);
    html.push('</font>');
    html.push(' ');
    html.push(unit);
    html.push('</p>');
    html.push('<p style=\"font-size:0.8em; font-style: italic\">Data Updated at: '); 
    html.push(date);
    html.push('</p>');
    return html.join('');
  }
  
  function generateCheckDate() {
    var html = [];
    var checkDate = new Date();
    var months = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
    html.push("<p style = \"font-size:0.8em; font-style: italic\">Last check was at: ");
    html.push(months[checkDate.getMonth()]);
    html.push(" " + checkDate.getDate() + ", " + checkDate.getFullYear() + " ");
    html.push(checkDate.toLocaleTimeString());
    html.push("</p>");
    return html.join('');
  }
  
  return {
    draw : sendQuery
  };
};